<template>
  <div class="login-wrap">
      <div class="ms-login">
          <div class="ms-title">技术中心监控管理系统</div>
           <!--登录表单区域 -->
          <el-form  :model="LoginForm"
          :rules="LoginFormRules"
          ref="LoginFormRef"
          label-width="0px"
          class="ms-content" >

          <el-form-item prop="username">
             <!--用户名区域 -->
             <el-input v-model="LoginForm.username" placeholder="username">
                <el-button slot="prepend" icon="iconfont icon-yonghudenglu"></el-button>
            </el-input>
          </el-form-item>
            <!--密码区域 -->
          <el-form-item prop="password">
              <el-input v-model="LoginForm.password" placeholder="password">
                  <el-button slot="prepend" icon="iconfont icon-mima"></el-button>
              </el-input>

          </el-form-item>
              <div class="login-btn">
                  <el-button type="primary" @click="submitForm()">登录</el-button>
              </div>
              <p class="login-tips">Tips : 请输入用户名和密码。</p>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      // 登录表单的数据对象
      LoginForm: {
        username: '',
        password: ''
      },
      // 这是表单的验证规则对象
      LoginFormRules: {
        // 验证用户名 属性
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        // 验证密码 属性
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      // 表单预校验
      this.$refs.LoginFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        let { data: result } = await this.$post('index/login/', this.LoginForm)
        console.log(result)
        if (result.meta.status !== 200) {
          console.log('登录失败')
          this.$message.error('登录失败！')
          return false
        }
        console.log('登录成功！')
        this.$message.success('登录成功！')
        // 1.将登录成功后的token ，保存到客户端的sessionstorage中
        this.$store.dispatch('dologin', result.data) // vuex 登录
        // 2.通过编程式导航，跳转到后台主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  /*background-image: url(../../assets/img/login-bg.jpg);*/
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
